/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-12-31 下午12:04:51
 */
package com.absir.server.socket;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 * @author absir
 * 
 */
public class SocketBuffer {

	/** id */
	private Serializable id;

	/** length */
	private int length;

	/** lengthIndex */
	private int lengthIndex;

	/** buffer */
	private byte[] buff;

	/** buffLengthIndex */
	private int buffLengthIndex;

	/** serverContext */
	private IServerContext serverContext;

	/** queueMax */
	private int queueMax;

	/** bufferQueue */
	private List<byte[]> bufferQueue;

	/**
	 * @return the id
	 */
	public Serializable getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	protected void setId(Serializable id) {
		this.id = id;
	}

	/**
	 * @return the length
	 */
	public int getLength() {
		return length;
	}

	/**
	 * @param length
	 *            the length to set
	 */
	public void setLength(int length) {
		this.length = length;
	}

	/**
	 * @return the lengthIndex
	 */
	public int getLengthIndex() {
		return lengthIndex;
	}

	/**
	 * @param lengthIndex
	 *            the lengthIndex to set
	 */
	public void setLengthIndex(int lengthIndex) {
		this.lengthIndex = lengthIndex;
	}

	/**
	 * @return the buff
	 */
	public byte[] getBuff() {
		return buff;
	}

	/**
	 * @param buff
	 *            the buff to set
	 */
	public void setBuff(byte[] buff) {
		this.buff = buff;
	}

	/**
	 * @return the buffLengthIndex
	 */
	public int getBuffLengthIndex() {
		return buffLengthIndex;
	}

	/**
	 * @param buffLengthIndex
	 *            the buffLengthIndex to set
	 */
	public void setBuffLengthIndex(int buffLengthIndex) {
		this.buffLengthIndex = buffLengthIndex;
	}

	/**
	 * @return the serverContext
	 */
	public IServerContext getServerContext() {
		return serverContext;
	}

	/**
	 * @param serverContext
	 *            the serverContext to set
	 */
	public void setServerContext(IServerContext serverContext) {
		this.serverContext = serverContext;
	}

	/**
	 * @param queueMax
	 *            the queueMax to set
	 */
	public void setQueueMax(int queueMax) {
		if (queueMax == 0) {
			bufferQueue = null;

		} else {
			if (bufferQueue == null) {
				bufferQueue = new LinkedList<byte[]>();
			}
		}

		this.queueMax = queueMax;
	}

	/** queueWaite */
	private boolean queueWaiting;

	/**
	 * @param buff
	 * @return
	 */
	public boolean addBufferQueue(byte[] buff) {
		if (bufferQueue != null) {
			synchronized (this) {
				if (queueWaiting) {
					if (bufferQueue.size() >= queueMax) {
						bufferQueue.remove(0);
					}

					bufferQueue.add(buff);
					buff = null;
					return true;

				} else {
					queueWaiting = true;
				}
			}
		}

		return false;

	}

	/**
	 * @return
	 */
	public byte[] readBufferQueue() {
		if (bufferQueue != null && queueWaiting) {
			synchronized (this) {
				if (queueWaiting) {
					if (!bufferQueue.isEmpty()) {
						return bufferQueue.remove(0);
					}

					queueWaiting = false;
				}
			}
		}

		return null;
	}
}
